*******************************************************************************
* Load polls data
*******************************************************************************

use dataset_polls, clear

*******************************************************************************
* Keep only polls where trust asked
*******************************************************************************

drop if mi(trustc) & mi(trustflc)

*******************************************************************************
* Get the micro-data
*******************************************************************************

local i = 1
levelsof date_house, local(levels)
foreach l of local levels {
	if `i' == 1 use "polls/`l'", clear
	if `i' >= 2 append using "polls/`l'"
	local i = `i' + 1
}

split date_house, parse("-")
gen date = date(date_house1 + date_house2 + date_house3, "YMD")

*******************************************************************************
* Drop if no microdata
*******************************************************************************

egen nsize = count(legitimatec), by(date_house)
keep if nsize > 10

*******************************************************************************
* Get the correlations for each sample/party
*******************************************************************************

foreach subsample in Democrat Republican {

	foreach x in trustc trustflc {

		gen cor_`x'`subsample' = .

		levelsof date_house, local(levels)
		foreach l of local levels {
			capture cor `x' legitimatec if date_house == "`l'" ///
				& party == "`subsample'"
			replace cor_`x'`subsample' = r(rho) if ///
				date_house == "`l'" 
		}

	}

}

*******************************************************************************
* Collapse to poll-subsample level
*******************************************************************************

collapse cor*, by(date_house date)

*******************************************************************************
* Add election cycle variable
*******************************************************************************

gen cycle = .
replace cycle = 2000 if year(date) < 2005
replace cycle = 2004 if date == date("2004-11-21", "YMD")
replace cycle = 2016 if year(date) > 2005 & year(date) < 2019
replace cycle = 2020 if year(date) > 2019

*******************************************************************************
* Plot
*******************************************************************************

format date %d

foreach subsample in Dem Rep {

	preserve

	drop if mi(cor_trustc`subsample') & mi(cor_trustflc`subsample')

	#delimit;

	gr tw
		(sc cor_trustc`subsample' date, msym(Oh) col(black))
		(sc cor_trustflc`subsample' date, msym(th) col(black))
		,
			legend(off)
			plotregion(style(none))
			ylab(, angle(horiz) labcol(black))
			xtitle("")
			by(cycle, xrescale note("") legend(off) 
				title(`subsample', col(black)) rows(2))
			subtitle(, fcol(none) lwid(none) col(black))
			name(g`subsample', replace)
			xlab(, labsize(small) labcol(black))
			xscale(lcol(black))
			yscale(lcol(black))
			;
		
	#delimit cr

	restore

}


*******************************************************************************
* Combine plots
*******************************************************************************

gr combine gDem gRep, xsize(3) ysize(2)

*******************************************************************************
* Save
*******************************************************************************

gr export "_output/figure_appendix_trust_cor.pdf", replace

*******************************************************************************
* End
*******************************************************************************
